﻿using DramaLoad.Domain;
using DramaLoad.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace DramaLoad.Controllers
{
    public class UserAreaController : Controller
    {
        DLContext db = new DLContext();
        // GET: UserArea
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Message()
        {
            return View();
        }

        public ActionResult Chpwd()
        {
            return View();
        }

        public ActionResult Chemail()
        {
            return View();
        }

        public ActionResult TopUp()
        {
            return View();
        }

        public ActionResult TopUpLanding()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection fc)
        {
            FormsAuthentication.SetAuthCookie("UserName",false);
            var pp = HttpContext.User.Identity.IsAuthenticated;
            return RedirectToAction("Index", "Home");
            //return View();
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var account = new Account { Username = model.usernameReg, Password = model.passwordReg,EmailAddress = model.emailText,BirthYear = model.birthYear };
                var pp = db.Accounts.Where(u => u.Username == account.Username).ToList();
                if (pp.Count>0)
                {
                    ModelState.AddModelError("", "用户已经存在");
                }
                else
                {
                    var result = db.Accounts.Add(account);
                    db.SaveChanges();
                }
                //if (result.Succeeded)
                //{
                //    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                //    // 有关如何启用帐户确认和密码重置的详细信息，请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                //    // 发送包含此链接的电子邮件
                //    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //    // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");

                //    return RedirectToAction("Index", "Home");
                //}
                //AddErrors(result);
            }

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }
        
        public ActionResult LostPwd()
        {
            return View();
        }
    }
}